package com.xinsoft.controller.qua;

import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.QuaMaterialInspectionDTO;
import com.xinsoft.entity.dto.QuaMaterialInspectionParam;
import com.xinsoft.service.QuaMaterialInspectionService;
import com.xinsoft.service.SysDocumentNumService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/qua/inspection")
public class QuaMaterialInspectionController {

    @Autowired
    private QuaMaterialInspectionService quaMaterialInspectionService;

    @Autowired
    private SysDocumentNumService sysDocumentNumService;

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add")
    public Response<?> add(@RequestBody QuaMaterialInspectionDTO quaMaterialInspectionDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(quaMaterialInspectionDTO);
        Integer documentId = null;
        if(Objects.equals(Const.QUA_TYPE.QUA_CPJY,quaMaterialInspectionDTO.getInspectionType())){
            documentId = Const.DOCUMENT_ID.QUA_CPJY_DOCUMENT;
        }
        if(Objects.equals(Const.QUA_TYPE.QUA_LLJY,quaMaterialInspectionDTO.getInspectionType())){
            documentId = Const.DOCUMENT_ID.QUA_LLJY_DOCUMENT;
        }
        if(Objects.equals(Const.QUA_TYPE.QUA_FHJY,quaMaterialInspectionDTO.getInspectionType())){
            documentId = Const.DOCUMENT_ID.QUA_FHJY_DOCUMENT;
        }
        sysDocumentNumService.incrementLastSerialByDocumentId(documentId);
        return Response.succeed(quaMaterialInspectionService.addQuaMaterialInspection(quaMaterialInspectionDTO));
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody  QuaMaterialInspectionDTO quaMaterialInspectionDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(quaMaterialInspectionDTO);
        return Response.succeed(quaMaterialInspectionService.editQuaMaterialInspection(quaMaterialInspectionDTO));
    }

    /**
     * 获取质量检验
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(quaMaterialInspectionService.getQuaMaterialInspection(id));
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        quaMaterialInspectionService.del(ids);
        return Response.succeed();
    }

    /**
     * 审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/audit")
    public Response<?> audit(@RequestBody List<Integer> ids) {
        quaMaterialInspectionService.audit(ids);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/deAudit")
    public Response<?> deAudit(@RequestBody List<Integer> ids) {
        quaMaterialInspectionService.deAudit(ids);
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody QuaMaterialInspectionParam quaMaterialInspectionParam) {
        return Response.succeed(quaMaterialInspectionService.findPageListByParam(quaMaterialInspectionParam));
    }

    /**
     * 报工时 -点击首检按钮 生成检验单
     */
    @GetMapping("/firstSampleInspection")
    public Response<?> firstSampleInspection(@RequestParam(value = "id", defaultValue = "", required = true) Integer dispatchId,
                                            @RequestParam(value = "firstInspectionType", defaultValue = "", required = true) Integer firstInspectionType) {
        quaMaterialInspectionService.firstSampleInspection(dispatchId,firstInspectionType);
        return Response.succeed();
    }

    /**
     * 提交检验结果
     */
    @PostMapping("/commitInspection")
    public Response<?> commitInspection(@RequestBody  QuaMaterialInspectionDTO quaMaterialInspectionDTO) {
        quaMaterialInspectionService.commitInspection(quaMaterialInspectionDTO);
        return Response.succeed();
    }
}
